import 'package:flutter/material.dart';

import 'dart:async';

class AlertDialogDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => AlertDialogDemoState();
}

enum Action { CANCLE, OK }

class AlertDialogDemoState extends State<AlertDialogDemo> {
  Action action;

  Future _showAlertDialog() async {
    final selectAction = await showDialog(
        context: context,
        barrierDismissible: false, //点击对话框外部强制不能关闭
        builder: (BuildContext context) => AlertDialog(
              title: Text('ShowAlertDialog'),
              content: Text('onpress item do action'),
              actions: <Widget>[
                FlatButton(
                    onPressed: () {
                      Navigator.of(context).pop(Action.CANCLE);
                    },
                    child: Text('cancle')),
                FlatButton(
                    onPressed: () {
                      Navigator.of(context).pop(Action.OK);
                    },
                    child: Text('ok'))
              ],
            ));

    switch (selectAction) {
      case Action.CANCLE:
        setState(() {
          action = Action.CANCLE;
        });
        break;

      case Action.OK:
        setState(() {
          action = Action.OK;
        });
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AlertDialogDemo'),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('AlertDialog select action value is$action'),
            SizedBox(
              height: 16.0,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                RaisedButton(
                    color: Colors.blue,
                    child: Text('On Press',
                        style: TextStyle(color: Colors.white, fontSize: 12)),
                    onPressed: _showAlertDialog)
              ],
            )
          ],
        ),
      ),
    );
  }
}
